package com.ilop.permission;

import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.view.View;


import androidx.fragment.app.FragmentActivity;

import com.ilop.permission.adapter.DataBindingAdapter;
import com.ilop.permission.base.BottomTileDialog;
import com.ilop.permission.callback.OnPermissionCallBack;
import com.ilop.permission.databinding.DialogPermissionBinding;

import java.util.List;

/**
 * @author skygge
 * @date 6/16/22.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class PermissionDialog extends BottomTileDialog<DialogPermissionBinding> implements PermissionUtil.PermissionsCallBack, DataBindingAdapter.OnItemClickListener<PermissionEnum> {

    private int mPermissionType;

    private boolean isPushPermission;

    private FragmentActivity mActivity;

    private PermissionAdapter mAdapter;

    private OnPermissionCallBack mCallBack;

    public PermissionDialog() {
    }

    public PermissionDialog(FragmentActivity activity, int mPermissionType, OnPermissionCallBack callBack) {
        this.mPermissionType = mPermissionType;
        this.mActivity = activity;
        this.mCallBack = callBack;
    }

    @Override
    protected int initLayoutId() {
        return R.layout.dialog_permission;
    }

    @Override
    protected void initView() {
        setCancelable(false);
        mAdapter = new PermissionAdapter(mContext, this);
        mDBind.permissionList.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        List<PermissionEnum> mPermissionList = PermissionEnum.getPermissionsList(mPermissionType);
        mAdapter.submitList(mPermissionList);
    }

    @Override
    protected void initListener() {
        mDBind.permissionContinue.setOnClickListener(view -> {
            mDBind.permissionProgress.setVisibility(View.VISIBLE);
            mAdapter.refreshPermission();
            new Handler(Looper.getMainLooper()).postDelayed(()-> {
                mDBind.permissionProgress.setVisibility(View.GONE);
                if (mAdapter.getNotAllowList().size() == 0) {
                    if (mCallBack!=null) mCallBack.onPermissionRequest(true);
                    dismiss();
                } else onGetPermission(mAdapter.getNotAllowList().get(0));
            }, 1000);
        });

        mDBind.permissionClose.setOnClickListener(view -> {
            boolean allGranted = (mAdapter.getNotAllowList().size() == 0);
            if (mCallBack!=null) mCallBack.onPermissionRequest(allGranted);
            dismiss();
        });
    }

    @Override
    protected void getResultLauncher(Intent intent) {
        mAdapter.refreshPermission();
    }

    @Override
    public void onRequestAllGranted() {
        mAdapter.refreshPermission();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isPushPermission) mAdapter.refreshPermission();
        isPushPermission = false;
    }

    @Override
    public void onItemClick(int viewId, PermissionEnum item, int position) {
        onGetPermission(item);
    }

    /**
     * 去获取权限
     * @param permission 权限
     */
    private void onGetPermission(PermissionEnum permission) {
        switch (permission) {
            case PERMISSION_LOCATION:
                PermissionUtil.onSetLocationPermissions(mActivity, this);
                break;
            case PERMISSION_CAMERA:
                PermissionUtil.onSetCameraPermissions(mActivity, this);
                break;
            case PERMISSION_PUSH:
                isPushPermission = true;
                PermissionUtil.goToAppNotificationSettings(mContext);
                break;
        }
    }
}
